﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WoChu.RfClient.Entity;
using WoChu.CF.BaseFrame.Util;
using WoChu.RfClient.Common;

namespace WoChu.RfClient.Duty
{
    public partial class FrmProdOnShelves : Form
    {
        public FrmProdOnShelves()
        {
            InitializeComponent();
        }
        private static List<GoodsBoxes> _lstGoodsBoxes = null;
        public static List<GoodsBoxes> lstGoodsBoxes
        {
            get
            {
                if (_lstGoodsBoxes == null) _lstGoodsBoxes = new List<GoodsBoxes>();
                return _lstGoodsBoxes;
            }
            set { _lstGoodsBoxes = value; }
        }

        

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                if (string.IsNullOrEmpty(txtLocationCode.Text))
                {
                    MessageBox.Show("货位码不能为空！");
                    txtBoxCode.Focus();
                    return;
                }
                if (lstGoodsBoxes != null && lstGoodsBoxes.Count() > 0)
                {
                    if (WoChu.CF.BaseFrame.Util.MessageBoxEx.Question("是否提交？") == DialogResult.No) return;


                    OutPutParams<bool> re = Duty.ProdOnShelvesUIRule.NewInstance.SetProdOnShelves(LoginBean.Instance.USERCODE,txtLocationCode.Text, lstGoodsBoxes);

                    if (re.Params)
                    {
                        lstGoodsBoxes.Clear();
                        this.ProductOnShelvesSource.DataSource = null;
                        this.dbGridProductOnShelves.DataSource = this.ProductOnShelvesSource;
                        txtBoxCode.Text = string.Empty;
                        txtGoodsCode.Text = string.Empty;
                       txtQTY.Text = string.Empty;
                       txtLocationCode.Text = string.Empty;
                       txtBoxCode.Focus(); 
                        WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("上架提交成功！");

                    }
                    else
                    {
                        MessageBox.Show("提交失败！\n" + re.Message);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }
 

        private void txtQTY_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter || string.IsNullOrEmpty(txtQTY.Text)) return;
           
            try
            {
                string goodsCode = txtGoodsCode.Text;

                if ( lstGoodsBoxes.Count() <= 0)
                {
                        MessageBox.Show("请先扫描箱号");
                        return;
                }
                if (string.IsNullOrEmpty(goodsCode))
                {
                    MessageBox.Show("请先扫描箱号");
                    return;
                }

                

                GoodsBoxes curItem = lstGoodsBoxes.Find(x => x.GOODSCODE.Equals(goodsCode));
                if (curItem==null)
                    curItem = lstGoodsBoxes.Find(x => x.BARCODE.Equals(goodsCode));
                    if(curItem.INQTY<Convert.ToInt32(txtQTY.Text))
                        if (WoChu.CF.BaseFrame.Util.MessageBoxEx.Question("输入数据大于原来数量，是否继续？") == DialogResult.No) return;
                 
                    GoodsBoxes curNewItem = curItem;
                    lstGoodsBoxes.Remove(curItem);
                    curNewItem.INQTY = Convert.ToDecimal(txtQTY.Text);

                    lstGoodsBoxes.Add(curNewItem);
                    this.ProductOnShelvesSource.DataSource = null;
                    this.ProductOnShelvesSource.DataSource = lstGoodsBoxes;
                    this.dbGridProductOnShelves.DataSource = this.ProductOnShelvesSource;
                    this.dbGridProductOnShelves.Refresh();
                    this.txtGoodsCode.Focus();
                    this.txtGoodsCode.SelectAll();

            }
            catch (Exception ex)
            {
                TraceEx.Write(ex.Message);
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("修改数量失败!");
            }
        }

        private void txtBoxCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            string boxCode = txtBoxCode.Text.Trim();
            if (string.IsNullOrEmpty(boxCode))
            {
                MessageBox.Show("请扫描正确的箱号条码！");
                txtBoxCode.Focus();
                return;
            }
            GoodsBoxes lgitem = lstGoodsBoxes.Find(x => x.GOODSBOXCODE == boxCode);
            if (lgitem == null)
            {
                List<GoodsBoxes> _GoodsBoxes = Duty.ProdOnShelvesUIRule.NewInstance.GetGoodsBoxesByBoxCode(boxCode);

                if (_GoodsBoxes != null)
                {
                    foreach (var item in _GoodsBoxes)
                    {
                        if(!lstGoodsBoxes.Any(x=>x.GOODSID==item.GOODSID))
                            lstGoodsBoxes.Add(item);
                    }

                   
                }
                else
                {
                    MessageBox.Show("没有找到匹配的数据！");
                    txtBoxCode.SelectAll();
                    return;
                }
            }

            this.ProductOnShelvesSource.DataSource = null;
            this.ProductOnShelvesSource.DataSource = lstGoodsBoxes;
            this.dbGridProductOnShelves.DataSource = this.ProductOnShelvesSource;
            this.dbGridProductOnShelves.Refresh();


            txtGoodsCode.Focus();
            txtGoodsCode.SelectAll(); 
        }

    

        private void txtGoodsCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter || string.IsNullOrEmpty(txtGoodsCode.Text)) return;
            

            //if (!goodsCode.StartsWith("WOC"))
            //{
            //    MessageBox.Show("请扫描正确的上架WOC条码！");
            //    txtGoodsCode.SelectAll();
            //    return;
            //}
            this.ProductOnShelvesSource.DataSource = null;
            this.ProductOnShelvesSource.DataSource = lstGoodsBoxes;
            this.dbGridProductOnShelves.DataSource = this.ProductOnShelvesSource;
            this.dbGridProductOnShelves.Refresh();
            if (lstGoodsBoxes == null) return;
            if (lstGoodsBoxes.Count <= 0) return;
            string goodsCode = txtGoodsCode.Text.Trim();
            //设置已扫描的为加亮
            if (string.IsNullOrEmpty(goodsCode)) return;
            if (goodsCode.Length >= 17)
                goodsCode = goodsCode.Substring(0, goodsCode.Length - 8);
            int curIndex = lstGoodsBoxes.FindIndex(x => x.GOODSCODE == goodsCode);
            if(curIndex<0)
                curIndex=lstGoodsBoxes.FindIndex(x => x.BARCODE == goodsCode);
            if (curIndex < 0) return;
            txtLocationCode.Text= dbGridProductOnShelves[curIndex, 0].ToString();
            txtQTY.Text = dbGridProductOnShelves[curIndex, 3].ToString();
            this.dbGridProductOnShelves.Select(curIndex);

            //this.dbGridProductOnShelves.SelectionBackColor = Color.Green;
            txtQTY.Focus();
            txtQTY.SelectAll();
        }
 
        private void dbGridProductOnShelves_DoubleClick(object sender, EventArgs e)
        {
            int curIndex = dbGridProductOnShelves.CurrentRowIndex;
            if (curIndex < 0) return;
            try
            {
                string goodsCode = dbGridProductOnShelves[curIndex, 5].ToString();
                GoodsBoxes curItem = lstGoodsBoxes.Find(x => x.GOODSCODE == goodsCode);
                //string ltCode = curItem.LOCATIONCODE;
                string gbName = curItem.GOODSNAME;

                //string Message = string.Format("货位:{0}, 名称:{1}, 商品批次号:{2} \n 是否删除该数据？", ltCode, gbName, gbCode);
                if (MessageBoxEx.Question("商品：" + gbName + " 是否删除该数据？") == DialogResult.Yes)
                {
                    lstGoodsBoxes.Remove(curItem);

                    this.ProductOnShelvesSource.DataSource = null;
                    this.ProductOnShelvesSource.DataSource = lstGoodsBoxes;
                    this.dbGridProductOnShelves.DataSource = this.ProductOnShelvesSource;
                    this.dbGridProductOnShelves.Refresh();
                }
            }
            catch (Exception ex)
            {
                TraceEx.Write(ex.Message);
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("删除数据失败!");
            }
            finally
            {
                txtBoxCode.Focus();
                txtBoxCode.SelectAll();
            }
        }

        private void FrmProductOnShelves_Closed(object sender, EventArgs e)
        {
             lstGoodsBoxes = null;
        }

        private void dbGridProductOnShelves_Click(object sender, EventArgs e)
        {

            int curIndex = dbGridProductOnShelves.CurrentRowIndex;
            if (curIndex < 0) return;
          txtGoodsCode.Text = dbGridProductOnShelves[curIndex, 5].ToString();
          txtLocationCode.Text = dbGridProductOnShelves[curIndex, 0].ToString();
          txtQTY.Text = dbGridProductOnShelves[curIndex, 3].ToString();
          this.dbGridProductOnShelves.Select(curIndex);
        }

    
    }
}